﻿Public Class ColorSelector

  Public Sub New()
    InitializeComponent()
    Me.TrackBarRed.Value = 0
    Me.TrackBarGreen.Value = 0
    Me.TrackBarBlue.Value = 0
  End Sub

  Public Sub New(ByVal clr As Color)
    Me.New(clr.R, clr.G, clr.B)
  End Sub

  Public Sub New(ByVal r As Integer, ByVal g As Integer, ByVal b As Integer)
    Me.New()

    If r > 255 Then r = 255
    If r < 0 Then r = 0
    If g > 255 Then g = 255
    If g < 0 Then g = 0
    If b > 255 Then b = 255
    If b < 0 Then b = 0

    Me.TrackBarRed.Value = r
    Me.TrackBarGreen.Value = g
    Me.TrackBarBlue.Value = b
  End Sub

  Private Sub NumericUpDownRed_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDownRed.ValueChanged
    If Me.TrackBarRed.Value <> NumericUpDownRed.Value Then
      Me.TrackBarRed.Value = NumericUpDownRed.Value
    End If

    Me.PanelPreview.BackColor = Color.FromArgb(Me.TrackBarRed.Value, Me.TrackBarGreen.Value, Me.TrackBarBlue.Value)
  End Sub

  Private Sub TrackBarRed_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBarRed.ValueChanged
    If Me.TrackBarRed.Value <> NumericUpDownRed.Value Then
      Me.NumericUpDownRed.Value = Me.TrackBarRed.Value
    End If

    Me.PanelPreview.BackColor = Color.FromArgb(Me.TrackBarRed.Value, Me.TrackBarGreen.Value, Me.TrackBarBlue.Value)
  End Sub

  Private Sub NumericUpDowngreen_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDownGreen.ValueChanged
    If Me.TrackBarGreen.Value <> NumericUpDownGreen.Value Then
      Me.TrackBarGreen.Value = NumericUpDownGreen.Value
    End If

    Me.PanelPreview.BackColor = Color.FromArgb(Me.TrackBarRed.Value, Me.TrackBarGreen.Value, Me.TrackBarBlue.Value)
  End Sub

  Private Sub TrackBargreen_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBarGreen.ValueChanged
    If Me.TrackBarGreen.Value <> NumericUpDownGreen.Value Then
      Me.NumericUpDownGreen.Value = Me.TrackBarGreen.Value
    End If

    Me.PanelPreview.BackColor = Color.FromArgb(Me.TrackBarRed.Value, Me.TrackBarGreen.Value, Me.TrackBarBlue.Value)
  End Sub

  Private Sub NumericUpDownblue_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDownBlue.ValueChanged
    If Me.TrackBarBlue.Value <> NumericUpDownBlue.Value Then
      Me.TrackBarBlue.Value = NumericUpDownBlue.Value
    End If

    Me.PanelPreview.BackColor = Color.FromArgb(Me.TrackBarRed.Value, Me.TrackBarGreen.Value, Me.TrackBarBlue.Value)
  End Sub

  Private Sub TrackBarblue_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBarBlue.ValueChanged
    If Me.TrackBarBlue.Value <> NumericUpDownBlue.Value Then
      Me.NumericUpDownBlue.Value = Me.TrackBarBlue.Value
    End If

    Me.PanelPreview.BackColor = Color.FromArgb(Me.TrackBarRed.Value, Me.TrackBarGreen.Value, Me.TrackBarBlue.Value)
  End Sub

  Public ReadOnly Property SelectedColor() As Color
    Get
      Return Color.FromArgb(Me.TrackBarRed.Value, Me.TrackBarGreen.Value, Me.TrackBarBlue.Value)
    End Get
  End Property

  Private Sub MenuItemSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemSelect.Click
    Me.DialogResult = Windows.Forms.DialogResult.OK
    Me.Close()
  End Sub

  Private Sub MenuItemCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemCancel.Click
    Me.DialogResult = Windows.Forms.DialogResult.Cancel
    Me.Close()
  End Sub
End Class